<template>
  <el-table :data="tableData2" style="width: 100%" border stripe>
    <el-table-column type="index"></el-table-column>
    <el-table-column prop="date" label="日期" width="180"> </el-table-column>
    <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
    <el-table-column prop="address" label="地址"> </el-table-column>
    <el-table-column prop="state" label="状态">
      <template slot-scope="scoped">
        <el-switch v-model="scoped.row.state" @change="changestate(scoped.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180px">
      <template slot-scope="scoped">
        <el-tooltip effect="dark" content="编辑角色" placement="top-start" :enterable="false">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="editsuser(scoped.row.id)"></el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="删除角色" placement="top" :enterable="false">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deluser(scoped.row.id)"></el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="分配角色" placement="top-end" :enterable="false">
          <el-button type="warning" icon="el-icon-setting" size="mini" @click="opendispathdialog(scoped.row)"></el-button>
        </el-tooltip>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { edituser, deluser, setroles } from '../api/index.js'
export default {
  data() {
    return {}
  },
  methods: {
    ...mapMutations(['adduserList', 'getusermenu', 'changesState', 'changeeditdialog', 'setedituserForm', 'changedispathdialog', 'getrightsuser', 'setsetroles']),
    changestate(val) {
      this.changesState(val)
    },
    async opendispathdialog(role) {
      // console.log(role)
      const { data: res } = await setroles()
      if (res.meta.status === 200) {
        // console.log(res)
        this.setsetroles(res.data)
        this.getrightsuser(role)
        this.changedispathdialog(true)
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    async editsuser(id) {
      this.changeeditdialog(true)
      // console.log(id)
      const { data: res } = await edituser(id)
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        // this.$message.success(res.meta.msg)
        // this.edituserForm = res.data
        this.setedituserForm(res.data)
      }
    },
    deluser(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await deluser(id)
          // console.log(res)
          if (res.meta.status === 200) {
            this.getusermenu()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.meta.msg
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.getusermenu()
  },
  computed: {
    ...mapState({
      queryinfo: state => state.Users.queryInfo,
      userList: state => state.Users.userList,
      total: state => state.Users.total,
      tableData2: state => state.Users.tableData2,
      edituserForm: state => state.Users.edituserForm
    })
  }
}
</script>

<style></style>
